package com.songlanyun.modules.account.entity;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.util.ObjectUtil;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.crypto.hash.Sha256Hash;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.songlanyun.common.drive.RootEntity;
import com.songlanyun.common.enums.GoodsConstant;
import com.songlanyun.common.enums.MemberConstant;
import com.songlanyun.common.utils.ShareCodeUtils;
import com.songlanyun.modules.account.service.UserVerifiedService;
import com.songlanyun.modules.rabbitmq.util.SpringContextUtil;

import cn.hutool.core.util.DesensitizedUtil;
import lombok.Data;

/**
 * 会员帐号表（会员的基本信息与关系）
 *
 * @author Mark
 * @email sunlightcs@gmail.com
 * @date 2020-11-02 15:51:10
 */
@Data
@TableName("yxt_account")
public class AccountEntity extends RootEntity<Long, AccountEntity> implements Serializable {
    private static final long serialVersionUID = 1L;

    public AccountEntity() {}

    /**
     * 初始化会员信息
     *
     * @param mobile
     * @param parentUserId
     */
    public AccountEntity(String mobile, Long parentUserId, String parentPath) {
        this.mobile = mobile;// 手机
        this.invitationCode = ShareCodeUtils.idToCode(Long.parseLong(mobile));// 邀请码
        this.parentUserId = parentUserId;// 推荐用户id，平台没有推荐会员，设为0
        this.parentPath = parentPath;// 平台会员设置为，

    }

    public static void main(String[] args) {
        System.out.println(ShareCodeUtils.idToCode(Long.parseLong("18771101376")));
    }

    /**
     *
     */
    @TableId
    private Long id;
    /**
     * 手机号
     */
    private String mobile;

    //个人业绩
    private BigDecimal personalPerformance;

    //团队业绩
    private BigDecimal teamPerformance;

    //团队等级
    private Integer teamGrade;

    //董事等级
    private Integer directorGrade;

    /**
     * 是否是线下店长
     */
    private Integer oflineShopowner;

    /**
     * *广告观看次数
     */
    private Integer seeTimes;

    /**
     * 脱敏的手机号
     *
     * @return
     */
    public String getMobileDesensitized() {
        if (StringUtils.isNotEmpty(this.mobile)) {
            return DesensitizedUtil.mobilePhone(this.mobile);
        }
        return this.mobile;

    }

    /**
     * 用户名
     */
    private String username;
    /**
     * 密码
     */
    @TableField("`password`")
    @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
    private String password;

    /**
     * 盐
     */
    @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
    private String salt;

    /**
     * '锁定状态（0false是未锁定可以正常使用，1true锁定后不能使用'
     */
    private Boolean lockStatus;
    /**
     * 最后一次登录时间
     */
    private Date lastLoginTime;
    /**
     *
     */
    @TableField(fill = FieldFill.INSERT)
    private Date createTime;
    /**
     *
     */
    @TableField(fill = FieldFill.INSERT_UPDATE)
    private Date updateTime;
    /**
     * 推荐用户Id(父级id)
     */
    private Long parentUserId;
    /**
     * 隶属那个商家Id
     */
    private Long subjectionShopId;
    /**
     * 邀请码
     */
    private String invitationCode;
    /**
     * 二维码地址
     */
    private String qrcode;
    /**
     * 会员注册来源 0：App 1：Wx
     */
    private MemberConstant.RegisterSource registerSource;

    /**
     * 注册时间
     */
    private Date registerTime;

    /**
     * 上级推荐人id逗号拼接
     */
    private String parentPath;

    /**
     * 是否是网红
     */
    private Boolean isCelebrity;


    /**
     * 是否被激活
     */
    private Boolean activating = false;

    /**
     * 最后被激活的时间
     */
    private Date lastActivatingTime;

    /**
     * 等级
     */
    private MemberConstant.MemberGrade grade = MemberConstant.MemberGrade.GENERAL;


    /**
     * 是否认证成功
     *
     * @return
     */
    @TableField(exist = false)
    private Boolean isAuthSuccess = false;

    /**
     * 账户地址
     */
    private String accountAddress;

    public Boolean getAuthSuccess() {
        UserVerifiedService userVerifiedService = (UserVerifiedService)SpringContextUtil.getBean("userVerifiedService");
        UserVerifiedEntity entity = userVerifiedService.loadById(id, false);
        return ObjectUtil.isNotNull(entity);
    }

    @Override
    public Long getIdentity() {
        return getId();
    }

    /**
     * 给密码加密
     */
    public void passwordEn(String password) {
        // sha256加密
        String salt = RandomStringUtils.randomAlphanumeric(20);
        String pass = new Sha256Hash(password, salt).toHex();
        this.setSalt(salt);// 盐
        this.setPassword(pass);// 密码
    }

    /**
     * 上级推荐人id 数组
     */
    public List<Long> getParentPathArr() {
        String parentPath = this.getParentPath();// ,46,

        if (StringUtils.isNotBlank(parentPath)) {
            String[] newArr = parentPath.split(",");// ["","46"]
            // 从第二个逗号开始 ["46"]
            List<Long> collect =
                Arrays.stream(newArr).filter(StringUtils::isNotBlank).map(Long::valueOf).collect(Collectors.toList());
            if (collect.size() >= 1) {
                collect.remove(0);

                return collect;

            }

        }
        return new ArrayList<>();
    }

}
